package edu.uic.keyoutcomestrackertool;

import edu.uic.kott.db.KottDataSource;
import edu.uic.kott.model.Instructor;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class InstructorDetails extends Activity {

	private static final String LOGTAG = "KOTT";

	KottDataSource datasource;

	private String id;
	private String type;
	private String instructorName;
	private String phoneNo;
	private String emailAdd;
	private String officeLocation;

	private TextView instructorNameTextView;
	private TextView phoneNoTextView;
	private TextView emailAddTextView;
	private TextView officeLocationTextView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_instructor_details);

		instructorNameTextView = (TextView) findViewById(R.id.instructorNameTextView);
		phoneNoTextView = (TextView) findViewById(R.id.phoneNoTextView);
		emailAddTextView = (TextView) findViewById(R.id.emailAddTextView);
		officeLocationTextView = (TextView) findViewById(R.id.officeLocationTextView);

		datasource = new KottDataSource(this);
		Bundle extras = getIntent().getExtras(); 
		Instructor instructor = new Instructor();

		if (extras != null){
			id = extras.getString("instructorId");
			Log.i(LOGTAG,"Instructor id is:" + id);

			instructor = datasource.getInstructorTaDetails(id);
			type = instructor.getInstructorType();
			instructorName = instructor.getInstructorName();
			phoneNo = instructor.getPhoneNo();
			emailAdd = instructor.getEmailAdd();
			officeLocation = instructor.getOfficeLocation(); 

			Log.i(LOGTAG,id + " " + type + " " + instructorName + " " + phoneNo + " " + emailAdd + " " + officeLocation );

			instructorNameTextView.setText(instructorName);
			phoneNoTextView.setText(phoneNo);
			emailAddTextView.setText(emailAdd);
			officeLocationTextView.setText(officeLocation);
		}		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_edit_delete, menu);		
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getOrder() == 100){
			Log.i(LOGTAG,"EDIT menu item clicked");
			Intent intent = new Intent(InstructorDetails.this, AddEditInstructor.class);
			intent.putExtra("option", "update");
			intent.putExtra("id", id);
			intent.putExtra("type",type);
			intent.putExtra("name", instructorName);
			intent.putExtra("phone", phoneNo);
			intent.putExtra("email", emailAdd);
			intent.putExtra("officeLocation", officeLocation);
			startActivity(intent);
			finish();
		}
		else if(item.getOrder() == 200)
		{	
			Log.i(LOGTAG,"Calling DELETE instructor from Instructor Details class");
			AlertDialog.Builder builder = new AlertDialog.Builder(InstructorDetails.this);
			builder.setTitle(R.string.dialogTitle_delete);			//set message title
			builder.setMessage(R.string.dialogMsg_deleteInstructor); //set message content

			builder.setCancelable(true);
			builder.setNegativeButton(R.string.cancel, null);

			builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					if(datasource.deleteInstructorTa(id)){
						Intent intent = new Intent(InstructorDetails.this, Instructors.class);
						intent.putExtra("option", "delete");
						intent.putExtra("id", id);
						setResult(RESULT_OK, intent);
						finish();
					}					
				}
			});

			AlertDialog confirmDialog = builder.create();
			confirmDialog.show();
		}
		return super.onOptionsItemSelected(item);
	}
}
